package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //    分页查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name,
                       Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        //调用业务层查询
        PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
        //返回数据给前端
        return Result.success(pageBean);
    }


    //删除员工
    @DeleteMapping("/{ids}")
    public Result deleteEmpById(@PathVariable List<Integer> ids) {
        //调用业务层完成删除
        empService.deleteEmpById(ids);
        //响应结果给前端
        return Result.success();
    }

    //保存员工
    @PostMapping
    public Result saveEmp(@RequestBody Emp emp) {
        //调用Service层完成逻辑
        empService.saveEmp(emp);
        //响应给前端数据
        return Result.success();
    }


    //根据员工id找员工数据
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        //调用Service层完成逻辑
        Emp emp = empService.getById(id);
        //返回结果给前端
        return Result.success(emp);
    }


    //修改员工数据
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        //调用Service层完成功能
        empService.update(emp);
        //返回结果
        return Result.success();
    }

}
